package com.common.utils;

import flex.messaging.io.amf.ASObject;
import flex.messaging.io.amf.translator.ASTranslator;

/**
 * 
 * ASObject对象转换工具类
 * 
 */
public class ASObjectConvert
{
    /**
     * 将指定的ASObject转化为指定的泛型对象
     * 
     * @param <E>
     * @param sourceObject
     * @param targetObjectClass
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <E> E convertASObject2JavaObject(ASObject sourceObject,
            Class<E> targetObjectClass)
    {
        ASTranslator ast = new ASTranslator();
        return (E)ast.convert(sourceObject, targetObjectClass);
    }
    
    /**
     * 将指定的ASObject转化为简单的Object对象(JavaBean)
     * 
     * @param sourceObject
     * @return
     */
    public static Object convertASObject2JavaObject(ASObject sourceObject)
    {
        ASTranslator ast = new ASTranslator();
        Object targetObject = null;
        try
        {
            targetObject = ast.convert(sourceObject,
                    Class.forName(sourceObject.getType()));
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return targetObject;
    }
}
